package com.easy.auth.domain;

import com.easy.base.domain.EObject;

public class Command extends EObject {

	public static final String EXP_WORLD = "WORLD";

	public static final String TYPE_EXP = "EXP";
	public static final String TYPE_USER = "USER";
	public static final String TYPE_ROLE = "ROLE";
	public static final String TYPE_GROUP = "GROUP";

	public static final String CODE_ALL_YES = "111111111";
	public static final String CODE_ALL_NO = "000000000";

	public final static int SAVE = 0;
	public final static int REMOVE = 1;
	public final static int EDIT = 2;
	public final static int VIEW = 3;
	public final static int SAVE_AS = 4;
	public final static int IMPORT = 5;
	public final static int EXPORT = 6;
	public final static int RELEASE = 7;
	public final static int UN_RELEASE = 8;

	private String objectKey;
	private String authType;
	private String authKey;
	private String authCode;
	
	public Command() {
		
	}
	
	public Command(String oid) {
		super(oid);
	}

	public String getObjectKey() {
		return objectKey;
	}

	public void setObjectKey(String objectKey) {
		this.objectKey = objectKey;
	}

	public String getAuthType() {
		return authType;
	}

	public void setAuthType(String authType) {
		this.authType = authType;
	}

	public String getAuthKey() {
		return authKey;
	}

	public void setAuthKey(String authKey) {
		this.authKey = authKey;
	}

	public String getAuthCode() {
		return authCode;
	}

	public void setAuthCode(String authCode) {
		this.authCode = authCode;
	}
	
	public boolean hasCommand(int code) {
		if (this.authCode == null || this.authCode.length() < code) {
			return false;
		}
		
		return this.authCode.charAt(code) == '1';
	}
	
	public void merge(Command command) {
		if (this.authCode == null || command.authCode == null) {
			this.authCode = command.authCode;
			return;
		}
		
		if (this.authCode.length() != command.authCode.length()) {
			return;
		}
		
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < this.authCode.length(); ++i) {
			char c = this.authCode.charAt(i);
			
			if (c == '1') {
				buffer.append(c);
			} else {
				buffer.append(command.authCode.charAt(i));
			}
		}
		
		this.authCode = buffer.toString();
	}

}
